import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import $$ from 'utils';
import moment from "moment";

export default {
  namespace: 'device',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      const p = {
        ...payload,
        baseUrl: getBaseUrl(),
        process: (records) => {
          return records.map(record => {
            return {
              ...record,
              lastTime: !!record.lastTime ? moment(record.lastTime).format('YYYY-MM-DD HH:mm:ss'): "",
            };
          });
        }
      }

      yield put({
        type: 'baseGetPageInfo',
        payload: p
      });
    },

    // 保存和修改
    * save({payload}, {put}) {

      const {value} = payload;

      let data = {id: value.id, deviceId: value.deviceId, hospitalId: value.hospitalId, runMode: value.runMode};

      if ($$.isNumber(payload.value.hospitalName)){
        data = {...data, hospitalId: payload.value.hospitalName};
      }
      payload.value = data;

      yield put({
        type: 'baseSave',
        payload: {...payload, baseUrl: getBaseUrl()}
      });
    },


    // 删除 之后查询分页
    * delete({payload}, {call, put, select}) {

      yield put({
        type: 'baseDelete',
        payload: {...payload, baseUrl: getBaseUrl()}
      });
    }
  }

}

function getBaseUrl() {
  return "/device";
}

